import { Component, OnInit } from '@angular/core';
import { PlatformuserService } from '../../service/platformuser.service';
import { AgentcashserviceService } from '../../service/agentcashservice.service';
import { RoleService } from '../../service/role.service';
import { CommonService } from '../../service/common.service';
import { NzMessageService } from 'ng-zorro-antd';
import { RoleRight } from '../../../kalend';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-sysuerlist',
  templateUrl: './agentpersoninfo.component.html',
  styleUrls: ['./agentpersoninfo.component.scss'],
  providers: [PlatformuserService, RoleService, AgentcashserviceService]
})
export class AgentpersoninfoComponent implements OnInit {
  relatephone: any;
  LoginUser = RoleRight.getStorage();
  name: any;
  role: Array<any> = RoleRight.getStorage().roleName;
  issmall = false;
  pageIndex = 1;
  newpwd: any;
  pageSize = 15;
  cashmoney;
  total = 1;
  dataSet = [];
  loading = true;
  isOkLoading = false;
  isVisible = false;
  uppwdisVisible = false;
  roleItems: Array<any> = []
  doc: any = {
    type: '奖励账户'
  };
  status: any;
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '有效' }, { key: 0, value: '禁用' }];
  typelist = [{ key: '奖励账户', value: '奖励账户' }, { key: '招募账户', value: '招募账户' }];
  // tslint:disable-next-line:max-line-length
  constructor(private router: Router, private agentcashservice: AgentcashserviceService, private userservice: PlatformuserService, private roleservice: RoleService, private msg: NzMessageService, private commonservice: CommonService) {
    // 小天使不支持提现奖励账户
    if (this.role.length > 0 && this.role.findIndex((a) => { return a.indexOf('小天使') >= 0; }) >= 0) {
      this.issmall = true;
    }
  }

  ngOnInit() {
    this.roleservice.query().then((res) => {
      this.roleItems = res;
    });
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.userservice.getSingleUser({
      userid: this.LoginUser._id
    }).then((res) => {
      this.loading = false;
      // this.total = res.total;
      this.dataSet = res;
    });
  }

  // 修改密码
  uppwd(id): void {
    this.uppwdisVisible = true;
    this.userservice.queryById(id).then((res) => {
      this.doc = res;
      this.userservice.getcustomeruser({ customerid: res.customerid }).then((r) => {
        if (r) {
          this.doc.relatephone = r.phone;
        }
      });

    });
  }
  uphandleOk(): void {
    this.isOkLoading = true;
    if (this.newpwd) {
      this.userservice.updateuserpwd({ _id: this.LoginUser._id, newpwd: this.newpwd }).then((r) => {
        this.msg.info('修改成功');
        this.isOkLoading = false;
      });

    }
  }



  // 提现申请
  cashapply(id): void {
    this.isVisible = true;
    this.userservice.queryById(id).then((res) => {
      this.doc = res;
      this.doc.type = '奖励账户';
      this.doc.money = res.balance;
    });

  }
  handleOk(): void {
    if (parseFloat(this.cashmoney).toString() === 'NaN') {
      this.msg.warning('请输入正确提现金额！');
      return;
    } else if (Number(this.cashmoney) < 10) {
      this.msg.warning('提现金额不得低于10元！');
      return;
    } else if (this.doc.money < this.cashmoney) {
      this.msg.warning('提现金额不能大于余额！');
      return;
    } else {
      this.isVisible = false;
      this.agentcashservice.save({
        agentid: this.LoginUser._id, // 品牌大使id
        bank: this.doc.bank, // 银行开户行
        bankno: this.doc.bankno, // 银行卡号
        phone: this.doc.phone, // 品牌大使手机号
        totalcase: this.cashmoney, // 提现总金额
        casestatus: 0, // 提现状态
        auditstatus: 0 // 审核状态
      }).then((r) => {
        this.msg.info(r)
      });
      this.doc = {};
    }
  }


  invrecord(id) {
    this.userservice.queryById(id).then((res) => {
      if (res.customerid) {
        const navurl = 'sysset/inverhistory/' + res.customerid;
        this.router.navigate([navurl]);
      } else {
        this.msg.error('请联系管理员关联您的小程序账户');
      }
    });
  }

  getRelCustomer(customerId) {
    this.userservice.getcustomeruser({ customerid: customerId })
  }


  uphandleCancel(): void {
    this.uppwdisVisible = false;
    this.doc = {};
  }

  handleCancel(): void {
    this.isVisible = false;
    this.doc = {
      type: '奖励账户'
    };
  }


  /**新增 */
  add() {
    this.isVisible = true;
  }

  /**修改 */
  edit(id: any) {
    this.isVisible = true;
    this.userservice.queryById(id).then((res) => {
      this.doc = res;
      this.userservice.getcustomeruser({ customerid: res.customerid }).then((r) => {
        if (r) {
          this.doc.relatephone = r.phone;
        }
      });
    });
  }
  /**禁用 */
  lock(id: any) {
    this.commonservice.confirm('确定要禁用用户吗?', () => {
      this.userservice.lock(id).then((res) => {
        this.msg.success('用户已禁用！');
        this.searchData();
      });
    })
  }
  /**启用 */
  open(id: any) {
    this.commonservice.confirm('确定要启用用户吗?', () => {
      this.userservice.open(id).then((res) => {
        this.msg.success('用户已启用！');
        this.searchData();
      });
    })
  }

  change() {
    this.cashmoney = null;
    if (this.doc.type === '奖励账户') {
      this.doc.money = this.doc.balance;
    } else if (this.doc.type === '招募账户') {
      this.doc.money = this.doc.balancezm;
    }
  }

}

